/**
 * Wrapper class for the Java properties module
 * How to use:
 * 1. Pass the file that holds the properties using setFile
 * 2. Get a value using getValue
 */

package uhasselt.ontologyplugin;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class PropertyReader {
	
	Properties _properties;
	
	public PropertyReader()
	{
		_properties = null;
	}
	
	/**
	 * Get a value from the properties file given a key.
	 * The file must have been set using setFile(path) before this function can be used.
	 * @param key The key of the value we want to look up
	 * @return The value of the given key 
	 */
	public String getValue(String key)
	{
		assert(_properties != null);
		return _properties.getProperty(key);
		
	}
	
	/**
	 * Get a value from the properties file given a key and a file.
	 * @param key The key of the value we want to look up
	 * @param pathToFile The path to the properties file
	 * @return The value of the given key 
	 * @throws IOException 
	 */
	public String getValue(String key, String pathToFile)
	{
		setFile(pathToFile);
		return getValue(key);
	}
	
	/**
	 * This function is used to pass the file that holds the properties
	 * @param pathToFile Path to the properties file
	 * @throws IOException 
	 */
	public void setFile(String pathToFile)
	{
		FileInputStream inStr = null;
		try {
			inStr = new FileInputStream(pathToFile);
		} catch (FileNotFoundException e) {
			// file not found
			e.printStackTrace();
		}
		_properties = new Properties();
		try {
			_properties.load(inStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Returns a hashmap (thesaurus, server) version of the property file
	 * The property file must have been set before you can use this function!
	 * @return hashmap (thesaurus, server) version of the property file
	 */
	public HashMap<String, String> getPairs()
	{
		assert(_properties != null);
		HashMap<String, String> ret = new HashMap<String, String>();
		Set<Object> thesauri = _properties.keySet();
		Iterator it = (Iterator) thesauri.iterator();
		while(it.hasNext()) { 
			String property = (String) it.next();
			ret.put(property, _properties.getProperty(property));
		}
		return ret;
	}
}
